<template>
  <div class="search">
    <van-nav-bar fixed title="商品列表" left-arrow @click-left="$router.go(-1)" />

    <van-search
      readonly
      shape="round"
      background="#ffffff"
      :value="querySearch||'搜索商品'"
      show-action
      @click="$router.push('/search')"
    >
      <template #action>
        <van-icon class="tool" name="apps-o" />
      </template>
    </van-search>

    <!-- 排序选项按钮 -->
    <div class="sort-btns">
      <div class="sort-item" @click="getProListById" :class="{'active-sort':sortType=='id'}">综合</div>
      <div class="sort-item" @click="getProListBySales" :class="{'active-sort':sortType=='sales'}">销量<van-icon name="arrow-down" /></div>
      <div class="sort-item" @click="getProListByPrice" :class="{'active-sort':sortType=='price'}">价格<van-icon name="arrow-up" /></div>
    </div>

    <div class="goods-list">
      <GoodsItem v-for="item in proList" :key="item.id" :item="item"></GoodsItem>
    </div>
  </div>
</template>

<script>
import { getProList } from '@/api/product'
import GoodsItem from '@/components/GoodsItem.vue'
import { mapState, mapMutations } from 'vuex'
export default {
  name: 'SearchIndex',
  data () {
    return {
      page: [0], // 默认
      sortType: 'id', // 默认根据id搜索
      proList: [],

      pageId: [0],
      pagePrice: [0.00],
      pageSales: [Number.MAX_SAFE_INTEGER]
    }
  },
  methods: {
    ...mapMutations('search', ['setCategoryId']),
    async getProListCom (goodsName, page, sortType) {
      // const { data: { list, sortValues } } = await getProList({
      const { data } = await getProList({
        // categoryId: this.$route.query.categoryId,
        goodsName,
        page,
        sortType,
        categoryId: this.getCategoryId()
      })
      if (data) {
        this.proList = data.list.data // !== null || list.data.length > 0 ? list.data : []
        this.page = data.sortValues
      }
    },
    async getProListByPrice () {
      if (this.sortType !== 'price') {
        this.sortType = 'price'
        this.page = this.pagePrice
      }
      await this.getProListCom(this.querySearch, this.page, this.sortType)
    },
    async getProListById () {
      if (this.sortType !== 'id') {
        this.sortType = 'id'
        this.page = this.pageId
      }
      await this.getProListCom(this.querySearch, this.page, this.sortType)
    },
    async getProListBySales () {
      if (this.sortType !== 'sales') {
        this.sortType = 'sales'
        this.page = this.pageSales
      }
      await this.getProListCom(this.querySearch, this.page, this.sortType)
    },
    getCategoryId () {
      const categoryId = this.$route.query.categoryId || this.categoryId
      // categoryId为真说明是分类页进入的
      if (categoryId) {
        this.setCategoryId(categoryId)
      }
      return categoryId
    }
  },
  components: {
    GoodsItem
  },
  computed: {
    ...mapState('search', ['categoryId']),
    querySearch () {
      return this.$route.query.search
    }
  },
  async created () {
    // 分类页进入搜索列表需要对categoryId持久化
    // const { data: { list } } = await getProList({
    const { data } = await getProList({
      goodsName: this.querySearch,
      page: this.page,
      sortType: this.sortType,
      categoryId: this.getCategoryId()
    })
    if (data) {
      this.proList = data.list.data
    }
  }
}
</script>

<style lang="less" scoped>
.search {
  padding-top: 46px;
  ::v-deep .van-icon-arrow-left {
    color: #333;
  }
  .tool {
    font-size: 24px;
    height: 40px;
    line-height: 40px;
  }

  .sort-btns {
    display: flex;
    height: 36px;
    line-height: 36px;
    .sort-item {
      text-align: center;
      flex: 1;
      font-size: 16px;
    }
  }
}

/* 商品样式 */
.goods-list {
  background-color: #f6f6f6;
}

/* 选中的排序方法的活动样式 */
.active-sort {
  border-radius: 20%;
  font-weight: bolder;
  color: red;
}
</style>
